package com.cykj.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Configuration
public class RedisRateConfig {
    @Bean
    public KeyResolver userKeyResolver() {
        KeyResolver keyResolver =  new KeyResolver() {
            @Override
            public Mono<String> resolve(ServerWebExchange exchange) {
                //过滤规则： 根据 ip 地址进行限流
                String ip = exchange.getRequest().
                        getRemoteAddress().
                        getAddress().
                        getHostAddress();
                System.out.println("ip:"+ip);
                return Mono.just(ip);
            }
        };
        return keyResolver;
    }
}
